package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.MarketAddress;
import com.cskaoyan.market.db.domain.MarketAddressExample;
import com.cskaoyan.market.db.mapper.MarketAddressMapper;
import com.cskaoyan.market.service.AdminAddressService;
import com.cskaoyan.market.util.MybatisUtil;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * Created with IntelliJ IDEA 2021.
 * User: Lazarus_Liu
 * Date: 2023/7/24
 * Time: 16:01
 * Description:
 */
public class AdminAddressServiceImpl implements AdminAddressService {
    @Override
    public List<MarketAddress> list(String page, String limit, String name, String userId, String sort, String order) {
        // 从数据库中查询相应的额的数据
        //  1. 获取sqlSession
        SqlSession session = MybatisUtil.getSession();
        MarketAddressMapper mapper = session.getMapper(MarketAddressMapper.class);
        // 2. 设置查询条件
        MarketAddressExample marketAddressExample = new MarketAddressExample();
        MarketAddressExample.Criteria criteria = marketAddressExample.createCriteria();
        // 当用户名非空时查询
        if (!StringUtils.isEmpty(name)) {
            criteria.andNameEqualTo(name);
        }
        // 当用户ID非空时查询
        if (!StringUtils.isEmpty(userId)) {
            criteria.andUserIdEqualTo(Integer.parseInt(userId));
        }
        // 3. 增加排序方式
        marketAddressExample.setOrderByClause(sort + " " + order);
        // 4. 分页
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));
        List<MarketAddress> marketAddresses = mapper.selectByExample(marketAddressExample);
        // 5. 提交事务
        session.commit();
        session.close();
        // 6. 返回list给controller
        return marketAddresses;
    }
}